// pages/selectcustomer/selectcustomer.js
const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    mindate:Math.round((new Date() - 365*86400*1000)),
    limit: 10,
    count: 0,
    flag: true, 
    select:false,
    date:"",
    show:false

  },
  onSearch(e){
    this.setData({
      flag:true
    })
    // console.log(this.data.mindate)
    this.getList(e.detail)
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onConfirm(event) {
    
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
    });
    this.getList("",this.data.date)
  },
  getList(search="",date="") {

    var that = this;
    var params = {
      page: that.data.page,
      limit: that.data.limit,
      search:search,
      select:Number(that.data.select),
      date:date
    }
    //页面功能定义
    request.requestAjax('invoice/getList', params, '加载中',function (res) {
      if (res.status === 200) {
        let list = that.data.list
        res.data.data.map((item) =>{
          return item.t = false;
        })
        if (that.data.page == 1) {
          list = []
        }
        list = [...list, ...res.data.data]
        if (list.length < res.data.count ){
          that.setData({
            page: that.data.page + 1,
            flag:true
          })
        } else {
          // console.log('1111');
          that.setData({
            flag:false
          })
        }
        that.setData({
          list: list,
          count: res.data.count
        })

      }
    }, function (res) {
    })
  },
  onReachBottom() {
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },

  goPage(event){
    // console.log(event);
    if(event.currentTarget.dataset.pageurl=="/pages/register/register"){
      Toast('暂未开放注册~');
    }

    wx.navigateTo({
      url: event.currentTarget.dataset.pageurl,
    })
  },
  del(index){
      var data = this.data.list[index];
      var params = {id:data.id}
      var that = this;
      request.requestAjax('invoice/del', params, '正在删除',function (res) {
        if (res.status === 200) {
          
          let list = that.data.list;
          list.splice(index,1);
          that.setData({
            list
          })
          wx.showToast({
            title: '删除成功',
            icon:'success'
          });
        }else{
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
      }, function (res) {
        wx.showToast({
          title: '删除失败',
          icon:'none'
        })
      })
  },
  onDel(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除吗？',
      success: function (res) {
        if (res.confirm) { //这里是点击了确定以后
          that.del(e.currentTarget.dataset.index);
        } 
      }
    })
  },
  onSelect(e){
    var data = this.data.list[e.currentTarget.dataset.index];
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];

    //页面功能定义
    var invoice = {
      id:data.id,
      currency:data.currency,
      invoice_no:data.invoice_no,
      fpze:data.fpze,
      currency:data.currency
    };
    prevPage.setData({
      invoice:invoice
    })
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
 
      if(options.select!=undefined){
        var select = options.select>=1?true:false;
      
        this.setData({select:select})
      }
      this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})